package com.health.adapter;

import java.util.List;

import com.health.R;
import com.health.tablebean.JTYSDoctorContInfoBean;
import com.health.viewscontrols.qyinfo.QYInfo;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 签约Adapter
 */
public class QYAdapter3 extends BaseAdapter {

    private Context mContext;
    public static final String QYINFO_JC = "基础服务";
    List<JTYSDoctorContInfoBean> list = null;
    private static SparseBooleanArray isSelected;
    private int flagSave;

    /**
     * 用SparseBooleanArray来代替map
     *
     * @param flagSave
     **/

    public QYAdapter3(Context mContext, List<JTYSDoctorContInfoBean> jtfwxmbs, int flagSave) {
        this.mContext = mContext;
        this.list = jtfwxmbs;
        isSelected = new SparseBooleanArray();
        initData();
        this.flagSave = flagSave;
    }

    private void initData() {
        int size = list.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                getIsSelected().put(i, false);
            }
        }

    }

    public static SparseBooleanArray getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(SparseBooleanArray isSelected) {
        QYAdapter3.isSelected = isSelected;
    }

    @Override
    public int getCount() {
        return list.size();

    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.qyinfo_list_item, null);
            vh = new ViewHolder();
            findView(vh, convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, vh);
        } else {
            vh = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        }

        initData(position, convertView, vh);
        return convertView;
    }

    /**
     * 初始化数据
     *
     * @param position
     * @param convertView
     * @param vh
     */
    private void initData(final int position, View convertView, final ViewHolder vh) {
        if (position % 2 == 0) {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.list_bg1));
        } else {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.list_bg2));
        }
        final JTYSDoctorContInfoBean fwxmb = list.get(position);
        vh.tongji_list_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                // holderView.cb_button.toggle();
                if (buttonView.isPressed()) {
                    isSelected.put(position, isChecked);
                }
                fwxmb.setSelected(isChecked);

            }
        });

        vh.tongji_list_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (QYInfo.SIGNTYPE_FAMILY.equals(QYInfo.signType)) {
                    Toast.makeText(mContext, "以家庭为单位的签约只能选择基础服务包", Toast.LENGTH_LONG).show();
                    vh.tongji_list_checkbox.setChecked(false);
                } else {
                    vh.tongji_list_checkbox.setChecked(true);
                }
            }
        });

        if (isSelected.get(position)) {
            vh.tongji_list_checkbox.setChecked(true);
        } else {
            vh.tongji_list_checkbox.setChecked(false);
        }
        setData(vh, convertView, fwxmb);
        if (flagSave == 1) {
            vh.tongji_list_checkbox.setEnabled(false);
            vh.tongji_list_checkbox.setClickable(false);
        }
    }

    /**
     * 赋值
     *
     * @param h
     * @param v
     * @author 王婷婷
     * @date 2015年12月3日 上午10:24:28
     */
    private void setData(ViewHolder h, View v, JTYSDoctorContInfoBean fwxmb) {
        if (fwxmb == null) {
            return;
        }
        h.tongji_list_xmb.setText(fwxmb.getContractServiceItemType());
        h.tongji_list_content.setText(fwxmb.getContractServiceItemContent());
        h.tongji_list_fwks.setText(fwxmb.getContractServiceItemKs());
        h.tongji_list_rqfl.setText(fwxmb.getPerson_type_name());
        String office_type = fwxmb.getOfficeType();
        //类型(0:一级医疗机构，1：二级医疗机构，2：三级医疗机构，3:四级机构；4：其他)
        if (!TextUtils.isEmpty(office_type)) {
            if ("0".equals(office_type)) {
                office_type = "一级医疗机构";
            } else if ("1".equals(office_type)) {
                office_type = "二级医疗机构";
            } else if ("2".equals(office_type)) {
                office_type = "三级医疗机构";
            } else if ("3".equals(office_type)) {
                office_type = "四级医疗机构";
            } else if ("4".equals(office_type)) {
                office_type = "其他";
            }
            h.tongji_list_lx.setText(office_type);
        } else {
            h.tongji_list_lx.setText("");
        }
        h.tongji_list_sfbz.setText(fwxmb.getFee_scale_desc());

        if (flagSave == 1) {
            h.tongji_list_checkbox.setEnabled(false);
            h.tongji_list_checkbox.setClickable(false);
        }
    }

    private void findView(ViewHolder h, View v) {
        h.tongji_list_xmb = (TextView) v.findViewById(R.id.tongji_list_xmb);
        h.tongji_list_content = (TextView) v.findViewById(R.id.tongji_list_content);
        h.tongji_list_checkbox = (CheckBox) v.findViewById(R.id.tongji_list_checkbox);
        h.tongji_list_fwks = (TextView) v.findViewById(R.id.tongji_list_fwks);
        h.tongji_list_rqfl = (TextView) v.findViewById(R.id.tongji_list_rqfl);
        h.tongji_list_lx = (TextView) v.findViewById(R.id.tongji_list_lx);
        h.tongji_list_sfbz = (TextView) v.findViewById(R.id.tongji_list_sfbz);
    }

    public static class ViewHolder {
        TextView tongji_list_xmb;
        TextView tongji_list_content;
        TextView tongji_list_fwks;
        TextView tongji_list_rqfl;
        TextView tongji_list_lx;
        TextView tongji_list_sfbz;
        public CheckBox tongji_list_checkbox;
    }

    /**
     * 获取被选中的数据
     *
     * @return
     * @author 王婷婷
     * @date 2017年6月1日 下午2:56:02
     */
    public String getCheckedDatas() {
        String ck_ids = "";
        int size = isSelected.size();
        Log.w("T---", "tt" + isSelected.size());
        Log.w("T---3", "tt" + list.size());
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                if (isSelected.get(i)) {
                    ck_ids = ck_ids + list.get(i).getId() + ",";
                }
            }
            if (!TextUtils.isEmpty(ck_ids)) {
                ck_ids = ck_ids.substring(0, ck_ids.length() - 1);
            }
        }

        return ck_ids;
    }

    /**
     * 设置选中的数据
     *
     * @return
     * @author 王婷婷
     * @date 2017年6月1日 下午2:56:13
     */
    public void setCheckedDatas(String ckidS) {
        if (TextUtils.isEmpty(ckidS)) {
            return;
        }
        Log.w(QYINFO_JC, ckidS);
        String ck_ids[] = ckidS.split(",");
        int length = ck_ids.length;
        int size = isSelected.size();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < size; j++) {
                    if (ck_ids[i].equals(list.get(j).getId())) {
                        // listChecks.get(j).setChecked(true);
                        Log.w(QYINFO_JC, ck_ids[i] + " iii " + j);
                        getIsSelected().put(j, true);
                    }
                }
            }

        }

    }
}
